package com.mofang.powerdekorhelper.activity.customer;

import android.content.Intent;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.mofang.powerdekorhelper.R;
import com.mofang.powerdekorhelper.activity.order.OrderDetailActivity;
import com.mofang.powerdekorhelper.adapter.ActivityCuponAdapter;
import com.mofang.powerdekorhelper.adapter.OrderAdapter;
import com.mofang.powerdekorhelper.base.MvpActivity;
import com.mofang.powerdekorhelper.base.recycleview.BaseRecycleViewList;
import com.mofang.powerdekorhelper.model.ActivityProcess;
import com.mofang.powerdekorhelper.model.CustomerActivityDetail;
import com.mofang.powerdekorhelper.model.Order;
import com.mofang.powerdekorhelper.persenter.CustomerActivityDetailPersenter;
import com.mofang.powerdekorhelper.utils.GlideLoader;
import com.mofang.powerdekorhelper.utils.LoadPrograss;
import com.mofang.powerdekorhelper.utils.ParamsUtils;
import com.mofang.powerdekorhelper.utils.T;
import com.mofang.powerdekorhelper.view.CustomerActivityDetailView;
import com.mofang.powerdekorhelper.witget.TitleBar;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * Created by luofei on 2017/9/2 0002.
 * 说明：
 */

public class CustomerActivityDetailAct extends MvpActivity<CustomerActivityDetailView, CustomerActivityDetailPersenter> implements CustomerActivityDetailView {

    @BindView(R.id.customer_activity_detail_title)
    TitleBar mTitleBar;

    @BindView(R.id.customer_activity_detail_imv)
    ImageView detailImv;

    @BindView(R.id.customer_activity_detail_name)
    TextView nameTv;

    @BindView(R.id.customer_activity_detail_state)
    TextView stateTv;

    @BindView(R.id.customer_activity_detail_gift)
    TextView giftTv;

    @BindView(R.id.customer_activity_detail_yprice)
    TextView ypriceTv;

    @BindView(R.id.customer_activity_detail_rprice)
    TextView rpriceTv;

    @BindView(R.id.customer_activity_detail_coupons)
    BaseRecycleViewList couponsRecycle;

    @BindView(R.id.customer_activity_detail_orders)
    BaseRecycleViewList ordersRecycle;

    private Integer activityId;

    private Integer customerId;

    LoadPrograss loadPrograss;

    @Override
    public CustomerActivityDetailPersenter initPresenter() {
        return new CustomerActivityDetailPersenter();
    }

    @Override
    public void initView() {

        inflateLayout(R.layout.activity_customer_activity_detail_layout);
        initTitleBarWithback(mTitleBar, R.string.activity_detial_title);

        loadPrograss = new LoadPrograss(this);

        couponsRecycle.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });

        ordersRecycle.setLayoutManager(new LinearLayoutManager(this) {
            @Override
            public boolean canScrollVertically() {
                return false;
            }
        });
    }

    @Override
    public void initData() {

        activityId = getIntent().getIntExtra("ActivityId", -1);

        customerId = getIntent().getIntExtra("CustomerId", -1);

        presenter.getActivityDetail(ParamsUtils.getCustomerActivityDetailParams(activityId, customerId));
    }

    @Override
    public void setCustomerActivityDetail(CustomerActivityDetail customerActivityDetail) {

        CustomerActivityDetail.Result result = customerActivityDetail.getResult();

        GlideLoader.getInstance(this).loadImage(result.getBlockurl(), detailImv);

        nameTv.setText("活动名称：" + result.getName());

        Integer state = result.getSign_state();

        String stateStr = "";

        if (state != null) {

            if (state == 1) {
                stateStr = "已签到";
            } else {
                stateStr = "未签到";
            }
            stateTv.setText("活动状态：" + stateStr);
        } else {
            stateTv.setText("活动状态：" + "未签到");
        }

        if (result.getSign_gift() != null) {
            giftTv.setText("签到礼：" + result.getSign_gift());
        } else {
            giftTv.setText("签到礼：" + "无");
        }
        ypriceTv.setText("已付总额：" + (int) result.getDeposit_price());
        rpriceTv.setText("实付金额：" + (int) result.getReal_pay_price() + "");

        //CustomerActivityDetailAdapter adapter = new CustomerActivityDetailAdapter()

        List<Order.Data> datas = new ArrayList<>();
        for (int i = 0; i < result.getOrder().size(); i++) {
            Order.Data data = new Order().new Data();
            data.setOrderId(result.getOrder().get(i).getId());
            data.setOrderCode(result.getOrder().get(i).getOrder_code());
            data.setOrderPrice(result.getOrder().get(i).getOrder_price());
            data.setCustomerName(result.getOrder().get(i).getConsignee_name());
            data.setCreateTime(result.getOrder().get(i).getCreate_time());
            data.setCustomerPhone(result.getOrder().get(i).getConsignee_phone());
            data.setActivityCode(result.getActivity_code());
            data.setOrderState(result.getOrder().get(i).getOrder_state());
            datas.add(data);
        }

        OrderAdapter adapter = new OrderAdapter(datas, this, R.layout.order_list_item_layout);

        adapter.setOrderClick(new OrderAdapter.IOrderClick() {

            @Override
            public void orderRefresh(int orderId, String orderCode, int activityId, int customerId) {


            }

            @Override
            public void orderDetail(int orderId) {

                startActivity(new Intent(CustomerActivityDetailAct.this, OrderDetailActivity.class).putExtra("OrderId", orderId));
            }

            @Override
            public void orderClose(int orderId, int orderState) {

            }
        });
        ordersRecycle.setAdapter(adapter);

        List<ActivityProcess.Itmes> cupons = new ArrayList<>();

        for (int i = 0; i < result.getItem().size(); i++) {
            ActivityProcess.Itmes itmes = new ActivityProcess().new Itmes();
            itmes.setActivity_id(result.getItem().get(i).getActivity_id());
            itmes.setUse_money(result.getItem().get(i).getUse_money());
            itmes.setCode(result.getItem().get(i).getCode());
            itmes.setCustomer_id(result.getItem().get(i).getCustomer_id());
            itmes.setGift(result.getItem().get(i).getGift());
            itmes.setId(result.getItem().get(i).getId());
            itmes.setItem_id(result.getItem().get(i).getItem_id());
            itmes.setOrder_id(result.getItem().get(i).getOrder_id());
            itmes.setStatus(result.getItem().get(i).getStatus());
            itmes.setGrade(result.getItem().get(i).getGrade());
            cupons.add(itmes);
        }

        ActivityCuponAdapter adapter1 = new ActivityCuponAdapter(cupons, this, R.layout.activity_coupons_list_item_layout);
        adapter1.setEnterFlag(1);
        couponsRecycle.setAdapter(adapter1);

        hideProgress();
    }

    @Override
    public void showProgress() {
        loadPrograss.show();
    }

    @Override
    public void hideProgress() {
        loadPrograss.hide();
    }

    @Override
    public void toast(CharSequence s) {

        T.showShort(this, s);
    }

    @Override
    public void toast(int id) {

    }

    @Override
    public void showNullLayout() {

    }

    @Override
    public void hideNullLayout() {

    }

    @Override
    public void showErrorLayout(View.OnClickListener listener) {

    }

    @Override
    public void hideErrorLayout() {

    }

    @Override
    public void onError(String errorInfo) {
        hideProgress();
        toast(errorInfo);
    }
}
